﻿using Hander.Customization;
using Hander.Customization.Frame.Classification;
using LiteDB;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace Hander.Forms.Frame
{
    public partial class ConfigurationWindow : FunctionFrm
    {
        public ConfigurationWindow()
        {
            InitializeComponent();
        }

        ClassDictionary GetCurrentContent()
        {
            ClassDictionary _rs = new ClassDictionary()
            {
                ChangedKeyContent = rtbxChangedKeyContent.Text,
                ClassBusinessCode = tbxBusinessCode.Text,
                ClassBusinessValue = tbxBusinessLeaderValue.Text,
                ClassTechCode = tbxTechCode.Text,
                ClassTechLeaderCode = tbxTechLeaderCode.Text,
                ClassType = tbxTypeCode.Text,
                UseableEndDate = dtpEndDate.Text,
                UseableStartDate = dtpStartDate.Text,
                ValidateFlag = cbxValidateFlag.Text
            };
            return _rs;
        }
        ///ClassDictionary Collection Name
        ///
        void QueryClassDictionaryList(ClassDictionary para)
        {
            var _db = LiteHelper.GetNewInstance();
            var _col = _db.GetCollection<ClassDictionary>("ClassDictionary");
            if(para is null)
            {
                //find all
                var _results = _col.FindAll();
                foreach (var _class in _results)
                {
                    Insert(_class);

                }

            }
            else
            {
                //var _results = _col.Find(Query.And(Query.EQ("", ""), Query.EQ("", "")));
                var _results = _col.FindAll();
                _results = _col.Find(x => x.ClassBusinessCode ==( string.IsNullOrEmpty(para.ClassBusinessCode) ? "" : para.ClassBusinessCode));
                foreach (var _class in _results)
                {
                    Insert(_class);

                }
            }

           

        }
        void Insert(ClassDictionary cd)
        {
            //dgvQueryList.col
            dgvQueryList.Rows.Add(cd.ClassTechCode,cd.ClassBusinessCode,cd.ClassBusinessValue,cd.ClassShortName,cd.UseableStartDate,cd.UseableEndDate,cd.ValidateFlag,cd.ClassType,cd.ClassTechLeaderCode);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void ConfigurationWindow_Load(object sender, EventArgs e)
        {
            cbxValidateFlag.Items.AddRange(Enum.GetNames(typeof(InnerClassType)));
            dtpStartDate.Text = "";
            dtpEndDate.Text = "";
        }

        private void BtnQueryList_Click(object sender, EventArgs e)
        {
            dgvQueryList.Rows.Clear();
            QueryClassDictionaryList(GetCurrentContent());

        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            var _db = LiteHelper.GetNewInstance();
            var _col = _db.GetCollection<ClassDictionary>("ClassDictionary");
            var _class = GetCurrentContent();
            string _value = _class?.ClassTechCode;
            if (string.IsNullOrEmpty(_value))
            {
                MessageAsync(MsgTip.tip_tip, $"空内容【ClassTechCode{_value}】");
                
                return;
            }
          
            if(_col.Exists(x => x.ClassTechCode == _value))
            {
                //
                if (_col.Update(_class))
                {
                    MessageAsync(MsgTip.tip_tip, "保存成功");
                }
            }
            else
            {               
                if (_col.Insert(_class))
                {
                    MessageAsync(MsgTip.tip_tip, "新增成功");
                }
            }
        }

        private void TsmiDelete_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确认删除吗？", "提示", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
            {
                //用户选择确认的操作
                var _techCode=dgvQueryList.CurrentRow.Cells[0].Value?.ToString();
                var _db = LiteHelper.GetNewInstance();
                var _col = _db.GetCollection<ClassDictionary>("ClassDictionary");
                
                int i = _col.Delete(x => x.ClassTechCode == _techCode);
                if (i>0)
                {
                    MessageAsync(MsgTip.tip_tip, $"删除成功成功,条数{i}");
                }
            }
            else if (dr == DialogResult.Cancel)
            {
                //用户选择取消的操作
               // MessageBox.Show("您选择的是【取消】");
            }
        }
    }
}
